<template>
  <div>
    <div class="gva-form-box">
      <el-form
        :model="formData"
        ref="elFormRef"
        label-position="right"
        :rules="rule"
        label-width="80px"
      >
        <el-form-item label="描述:" prop="description">
          <el-input v-model="formData.description" :clearable="false" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="规格:" prop="specs">
          // 此字段为json结构，可以前端自行控制展示和数据绑定模式 需绑定json的key为 formData.specs
          后端会按照json的类型进行存取
          {{ formData.specs }}
        </el-form-item>
        <el-form-item label="属性:" prop="attrs">
          // 此字段为json结构，可以前端自行控制展示和数据绑定模式 需绑定json的key为 formData.attrs
          后端会按照json的类型进行存取
          {{ formData.attrs }}
        </el-form-item>
        <el-form-item label="图片URL:" prop="imageUrl">
          <el-input v-model="formData.imageUrl" :clearable="false" placeholder="请输入图片URL" />
        </el-form-item>
        <el-form-item label="轮播图:" prop="banner">
          // 此字段为json结构，可以前端自行控制展示和数据绑定模式 需绑定json的key为 formData.banner
          后端会按照json的类型进行存取
          {{ formData.banner }}
        </el-form-item>
        <el-form-item label="价格:" prop="price">
          <el-input-number
            v-model="formData.price"
            :precision="2"
            :clearable="false"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="评分:" prop="rating">
          <el-input-number
            v-model="formData.rating"
            :precision="2"
            :clearable="false"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="评论数量:" prop="reviewCount">
          <el-input v-model.number="formData.reviewCount" :clearable="false" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="销售数量:" prop="saleCount">
          <el-input v-model.number="formData.saleCount" :clearable="false" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="名称:" prop="title">
          <el-input v-model="formData.title" :clearable="false" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型:" prop="categoryID">
          <el-input v-model.number="formData.categoryID" :clearable="false" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-switch
            v-model="formData.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            clearable
          ></el-switch>
        </el-form-item>
        <el-form-item label="邮费:" prop="postage">
          <el-input-number
            v-model="formData.postage"
            :precision="2"
            :clearable="false"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="折扣:" prop="discount">
          <el-input v-model.number="formData.discount" :clearable="false" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="SKU:" prop="skus">
          <ArrayCtrl v-model="formData.skus" editable />
        </el-form-item>
        <el-form-item label="详情:" prop="detail">
          <RichEdit v-model="formData.detail" />
        </el-form-item>
        <el-form-item label="收藏数量:" prop="collectNum">
          <el-input v-model.number="formData.collectNum" :clearable="false" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="销量:" prop="saleNum"></el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
  import { createGood, updateGood, findGood } from '@/plugin/Shop/api/good'

  defineOptions({
    name: 'GoodForm',
  })

  // 自动获取字典
  import { getDictFunc } from '@/utils/format'
  import { useRoute, useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { ref, reactive } from 'vue'
  // 富文本组件
  import RichEdit from '@/components/richtext/rich-edit.vue'
  // 数组控制组件
  import ArrayCtrl from '@/components/arrayCtrl/arrayCtrl.vue'

  const route = useRoute()
  const router = useRouter()

  const type = ref('')
  const formData = ref({
    description: '',
    specs: {},
    attrs: {},
    imageUrl: '',
    banner: {},
    price: 0,
    rating: 0,
    reviewCount: 0,
    saleCount: 0,
    title: '',
    categoryID: 0,
    status: false,
    postage: 0,
    discount: 0,
    skus: [],
    detail: '',
    collectNum: 0,
  })
  // 验证规则
  const rule = reactive({})

  const elFormRef = ref()

  // 初始化方法
  const init = async () => {
    // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findGood({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
  }

  init()
  // 保存按钮
  const save = async () => {
    elFormRef.value?.validate(async (valid) => {
      if (!valid) return
      let res
      switch (type.value) {
        case 'create':
          res = await createGood(formData.value)
          break
        case 'update':
          res = await updateGood(formData.value)
          break
        default:
          res = await createGood(formData.value)
          break
      }
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '创建/更改成功',
        })
      }
    })
  }

  // 返回按钮
  const back = () => {
    router.go(-1)
  }
</script>

<style></style>
